﻿using System;
using System.IO;
using System.Threading;
using System.Collections;

namespace InputTool
{
    public class XDLog
    {
        public enum StrType { ASCII = 0, BCD = 1 };

        private static string FilePath = "D:\\LOG\\";//系统日志文件保存路径
        private static object lockflag = new object();
        private static object cardlockflag = new object();

        #region 写系统日志
        /// <summary>
        /// 写系统日志
        /// </summary>
        /// <param name="FileName">日志生成的文件名</param>
        /// <param name="SpendFuncVal">日志发生的位置</param>
        /// <param name="strLog">日志内容</param>
        public static void WriteLog(string FileName, string SpendFuncVal, string strLog)
        {
            lock (lockflag)
            {
                if (!Directory.Exists("D:\\"))
                {
                    FilePath = "C:\\LOG\\";
                }
                string strFileName, strInfo, strTime;
                strFileName = System.DateTime.Now.ToString("yyyyMMdd") + "_" + FileName + ".log";
                string LogFilePath = FilePath + System.DateTime.Now.ToString("yyyyMM") + "\\" + System.DateTime.Now.ToString("dd");
                if (Directory.Exists(LogFilePath) == false)
                {
                    Directory.CreateDirectory(LogFilePath);
                }
                FileStream fs = new FileStream(LogFilePath + "\\" + strFileName, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

                strTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
                strInfo = string.Format("{0} 函数【{1}】 {2}", strTime, SpendFuncVal, strLog);
                m_streamWriter.WriteLine(strInfo);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
        }


        /// <summary>
        /// 写系统日志
        /// </summary>
        /// <param name="FileName">日志生成的文件名</param>
        /// <param name="SpendFuncVal">日志发生的位置</param>
        /// <param name="strLog">日志内容</param>
        public static void WriteLog(string FileName, string SpendFuncVal, byte[] byData,StrType mType)
        {
            if (!Directory.Exists("D:\\"))
            {
                FilePath = "C:\\LOG\\";
            }

            string strLog;
            if (StrType.ASCII == mType)
                strLog = XDConvert.Arr2Str(byData);
            else
                strLog = XDConvert.HexAscArr2Str(byData);

            lock (lockflag)
            {
                string strFileName, strInfo, strTime;
                strFileName = System.DateTime.Now.ToString("yyyyMMdd") + "_" + FileName + ".log";
                string LogFilePath = FilePath + System.DateTime.Now.ToString("yyyyMM") + "\\" + System.DateTime.Now.ToString("dd");
                if (Directory.Exists(LogFilePath) == false)
                {
                    Directory.CreateDirectory(LogFilePath);
                }
                FileStream fs = new FileStream(LogFilePath + "\\" + strFileName, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

                strTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
                strInfo = string.Format("{0} 函数【{1}】 {2}", strTime, SpendFuncVal, strLog);
                m_streamWriter.WriteLine(strInfo);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
        }

        #endregion

        #region 
        /// <summary>
        /// 卡日志
        /// </summary>
        /// <param name="FileName">日志生成的文件名</param>
        /// <param name="strLog">日志内容</param>
        public static void WriteCardLog(string FileName,  string strLog)
        {
            if (!Directory.Exists("D:\\"))
            {
                FilePath = "C:\\LOG\\";
            }

            lock (cardlockflag)
            {
                string strFileName, strInfo, strTime;
                strFileName = FileName + ".log";
                string LogFilePath = FilePath;
                if (Directory.Exists(LogFilePath) == false)
                {
                    Directory.CreateDirectory(LogFilePath);
                }
                FileStream fs = new FileStream(LogFilePath + "\\" + strFileName, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

                //strTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
                strInfo = string.Format("{0}", strLog);
                m_streamWriter.WriteLine(strInfo);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
        }
        #endregion
    }
}
